<?php

namespace Backoffice\Controller;

use Zend\View\Model\ViewModel;
use Zend\Mvc\Controller\AbstractActionController;

class BoutiqueController extends AbstractActionController
{
    /**
    * Entity manager instance
    *
    * @var Doctrine\ORM\EntityManager
    */
    protected $em;

    /**
    * Returns an instance of the Doctrine entity manager loaded from the service
    * locator
    *
    * @return Doctrine\ORM\EntityManager
    */
    
    public function getEntityManager()
    {
        if (null === $this->em) {
            $this->em = $this->getServiceLocator()
                ->get('doctrine.entitymanager.orm_default');
        }
        return $this->em;
    }
    
    public function indexAction()
    {
        $authorize = $this->getServiceLocator()->get('BjyAuthorize\Provider\Identity\ProviderInterface');
        $roles = $authorize->getIdentityRoles();
        $id = $this->zfcUserAuthentication()->getIdentity()->getId();
        
        // Admin logged in.
        if (in_array('admin', $roles)) {
            return array(
                'boutiques' => $this->getEntityManager()->getRepository('Backoffice\Model\Entity\Boutique')->findAll()
            );
        }
        
        // User logged in.
        $boutique = $this->getEntityManager()->find('Backoffice\Model\Entity\Utilisateur', $id)->getBoutique();
        if ($boutique->getEstActive() == 0){
            //$view1 = new ViewModel(array('id' => $boutique->getId()));
            //$view1->setTemplate('backoffice/boutique/active');
            //return $view1;
            $boutique->setEstActive(true);
            $this->getEntityManager()->flush();
        }
        $fsm = $this->getServiceLocator()->get('FormElementManager');
        $form = $fsm->get('Backoffice\Form\BoutiqueForm');
        $form->bind($boutique);
        
        $request = $this->getRequest();
        if ($request->isPost()) {
            $data = $request->getPost();
            $form->setData($data);
            
            if ($form->isValid()) {
                $this->getEntityManager()->persist($boutique);
                $this->getEntityManager()->flush();
            }
        }

        $view = new ViewModel(array(
            'boutique' => $boutique,
            'form' => $form,
        ));
        $view->setTemplate('backoffice/boutique/user-boutique');
        return $view;
    }
    
     /* ******************* 
        PARTIE USERS
    ******************* */
     public function activeAction()
    {
        $id = $this->params()->fromRoute('id');
        $boutique = $this->getEntityManager()->getRepository('Backoffice\Model\Entity\Boutique')->findBy(
    array('id' => $id));
        $boutique->setEstActive(true);
        $this->getEntityManager()->flush();
    }
    
    /* ******************* 
        PARTIE ADMIM
    ******************* */
    public function deleteAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('boutique');
        }

        $request = $this->getRequest();
        if ($request->isPost()) {
            $del = $request->getPost('del', 'No');

            if ($del == 'Yes') {
                $id = (int) $request->getPost('id');
                $boutique = $this->getEntityManager()->find('Backoffice\Model\Entity\Boutique', $id);
                if ($boutique) {
                    $this->getEntityManager()->remove($boutique);
                    $this->getEntityManager()->flush();
                }
            }

            // Redirect to list of boutiques
            return $this->redirect()->toRoute('boutique');
        }

        return array(
            'id'    => $id,
            'boutique' => $this->getEntityManager()->find('Backoffice\Model\Entity\Boutique', $id)
        );
    }
}

